package com.ylx.web;

import com.ylx.pojo.User;
import com.ylx.service.UserService;
import com.ylx.service.UserServiceIpml;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * Servlet implementation class LoginServlet
 */

@WebServlet("/loginsServlet")
public class Login1Servlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password=request.getParameter("password");

        UserService userService=new UserServiceIpml();
        User user = userService.select1(username,password);
        String error="";
        if(user!=null&&user.getPassword().equals(password)){
            HttpSession session = request.getSession();
            session.setAttribute("user", user);
            response.sendRedirect("productServlet");
        }
        else{
            error="用户名或者密码错误，请确认后重新登录";
            request.setAttribute("error", error);
            request.getRequestDispatcher("front/login.jsp").forward(request, response);
        }

    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
         this.doGet(request, response);
    }

}
